{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pickle\n",
    "from utils import *\n",
    "from FactorCalculation import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading raw data...\n",
      "Processing ret_5d...\n",
      "Processing ret_21d...\n",
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 配置参数\n",
    "DATA_LIST=['close']\n",
    "N_DAYS_LIST = [5, 21]\n",
    "\n",
    "# 加载数据\n",
    "print(\"Loading raw data...\")\n",
    "long_df = load_raw_data(DATA_LIST)\n",
    "\n",
    "# 计算并保存因子\n",
    "for n in N_DAYS_LIST:\n",
    "    \n",
    "    # 实例化因子\n",
    "    factor = ret(n_days=n, name='ret')\n",
    "    print(f\"Processing {factor.name}...\")\n",
    "    \n",
    "    # 计算因子值\n",
    "    factor_df = factor.compute(long_df)\n",
    "    \n",
    "    # 预处理\n",
    "    factor_df = winsorize(factor_df)\n",
    "    factor_df = standardlize(factor_df)\n",
    "    \n",
    "    # 保存结果\n",
    "    save_factor(factor_df, factor.name, n)\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            000001.SZ  000002.SZ  000004.SZ  000006.SZ  000007.SZ  000008.SZ  \\\n",
      "date                                                                           \n",
      "2002-01-04        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-07        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-08        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-09        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-10        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "...               ...        ...        ...        ...        ...        ...   \n",
      "2024-12-25   0.948647  -0.513221  -2.716498  -1.098721  -0.178136   1.498272   \n",
      "2024-12-26   0.825579  -0.319978  -2.700452  -1.632438   0.151428   0.982976   \n",
      "2024-12-27   0.888922  -0.015894  -2.876233  -1.157598   0.798895   1.092389   \n",
      "2024-12-30   0.444649  -0.670347  -2.471166  -1.734264   1.401351   1.227867   \n",
      "2024-12-31   0.475485  -0.481907  -2.252669  -1.510874   1.402215  -0.857548   \n",
      "\n",
      "            000009.SZ  000010.SZ  000011.SZ  000012.SZ  ...  688708.SH  \\\n",
      "date                                                    ...              \n",
      "2002-01-04        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-07        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-08        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-09        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-10        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "...               ...        ...        ...        ...  ...        ...   \n",
      "2024-12-25   0.313968  -0.666861  -0.202773   0.376657  ...        NaN   \n",
      "2024-12-26   0.073307  -1.051234  -0.374473   0.234951  ...        NaN   \n",
      "2024-12-27   0.433180  -0.727730   0.007093   0.578040  ...        NaN   \n",
      "2024-12-30   0.290816  -0.122661  -0.024070   0.228611  ...        NaN   \n",
      "2024-12-31   0.002947   0.605472   0.340133   0.298794  ...        NaN   \n",
      "\n",
      "            920098.BJ  301617.SZ  688605.SH  920106.BJ  301585.SZ  301598.SZ  \\\n",
      "date                                                                           \n",
      "2002-01-04        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-07        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-08        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-09        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-10        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "...               ...        ...        ...        ...        ...        ...   \n",
      "2024-12-25        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-26        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-27        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-30        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-31        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "\n",
      "            603194.SH  920082.BJ  001391.SZ  \n",
      "date                                         \n",
      "2002-01-04        NaN        NaN        NaN  \n",
      "2002-01-07        NaN        NaN        NaN  \n",
      "2002-01-08        NaN        NaN        NaN  \n",
      "2002-01-09        NaN        NaN        NaN  \n",
      "2002-01-10        NaN        NaN        NaN  \n",
      "...               ...        ...        ...  \n",
      "2024-12-25        NaN        NaN        NaN  \n",
      "2024-12-26        NaN        NaN        NaN  \n",
      "2024-12-27        NaN        NaN        NaN  \n",
      "2024-12-30        NaN        NaN        NaN  \n",
      "2024-12-31        NaN        NaN        NaN  \n",
      "\n",
      "[5579 rows x 5379 columns]\n"
     ]
    }
   ],
   "source": [
    "with open(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\ret_5d.pkl', 'rb') as file:\n",
    "    loaded_data = pickle.load(file)\n",
    "print(loaded_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            000001.SZ  000002.SZ  000004.SZ  000006.SZ  000007.SZ  000008.SZ  \\\n",
      "date                                                                           \n",
      "2002-01-04        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-07        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-08        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-09        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-10        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "...               ...        ...        ...        ...        ...        ...   \n",
      "2024-12-25   0.608881  -0.838735  -1.482280  -0.340710  -0.721922   2.621397   \n",
      "2024-12-26   0.499913  -0.894699  -1.061512  -0.218647  -0.189179   2.417971   \n",
      "2024-12-27   0.475183  -0.935597  -1.682510  -0.779555   0.102509   2.291171   \n",
      "2024-12-30   0.754114  -0.997181  -1.882755  -1.127163   0.203260   2.084128   \n",
      "2024-12-31   0.974880  -0.804913  -1.575298  -1.241081   0.376025   1.123641   \n",
      "\n",
      "            000009.SZ  000010.SZ  000011.SZ  000012.SZ  ...  688708.SH  \\\n",
      "date                                                    ...              \n",
      "2002-01-04        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-07        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-08        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-09        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-10        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "...               ...        ...        ...        ...  ...        ...   \n",
      "2024-12-25   0.224388   0.176123  -0.288066  -0.064066  ...        NaN   \n",
      "2024-12-26  -0.098594   0.429663  -0.316606  -0.062340  ...        NaN   \n",
      "2024-12-27  -0.326408  -0.392782  -0.793606  -0.158029  ...        NaN   \n",
      "2024-12-30  -0.127107  -0.470933  -0.415958   0.056742  ...        NaN   \n",
      "2024-12-31  -0.131150  -0.232595  -0.388506   0.141152  ...        NaN   \n",
      "\n",
      "            920098.BJ  301617.SZ  688605.SH  920106.BJ  301585.SZ  301598.SZ  \\\n",
      "date                                                                           \n",
      "2002-01-04        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-07        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-08        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-09        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-10        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "...               ...        ...        ...        ...        ...        ...   \n",
      "2024-12-25        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-26        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-27        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-30        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-31        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "\n",
      "            603194.SH  920082.BJ  001391.SZ  \n",
      "date                                         \n",
      "2002-01-04        NaN        NaN        NaN  \n",
      "2002-01-07        NaN        NaN        NaN  \n",
      "2002-01-08        NaN        NaN        NaN  \n",
      "2002-01-09        NaN        NaN        NaN  \n",
      "2002-01-10        NaN        NaN        NaN  \n",
      "...               ...        ...        ...  \n",
      "2024-12-25        NaN        NaN        NaN  \n",
      "2024-12-26        NaN        NaN        NaN  \n",
      "2024-12-27        NaN        NaN        NaN  \n",
      "2024-12-30        NaN        NaN        NaN  \n",
      "2024-12-31        NaN        NaN        NaN  \n",
      "\n",
      "[5579 rows x 5379 columns]\n"
     ]
    }
   ],
   "source": [
    "with open(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\ret_21d.pkl', 'rb') as file:\n",
    "    loaded_data = pickle.load(file)\n",
    "print(loaded_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "收益率计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            000001.SZ  000002.SZ  000004.SZ  000006.SZ  000007.SZ  000008.SZ  \\\n",
      "date                                                                           \n",
      "2002-01-04        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-07  -0.005805  -0.012312   0.001375  -0.024656  -0.003414  -0.019015   \n",
      "2002-01-08  -0.001656  -0.010157  -0.003659  -0.010378  -0.001151  -0.007117   \n",
      "2002-01-09  -0.010824  -0.010261  -0.016056  -0.011383  -0.050531  -0.048854   \n",
      "2002-01-10  -0.006706   0.005655   0.007459  -0.003808  -0.009651   0.035498   \n",
      "...               ...        ...        ...        ...        ...        ...   \n",
      "2024-12-25   0.005059  -0.024580  -0.099692  -0.061893  -0.026432   0.015873   \n",
      "2024-12-26  -0.005034  -0.003979   0.061517   0.001294   0.025641  -0.034375   \n",
      "2024-12-27  -0.002530   0.005326  -0.067611   0.036176   0.044118   0.003236   \n",
      "2024-12-30   0.010144  -0.025166  -0.050414  -0.067332   0.011268  -0.022581   \n",
      "2024-12-31  -0.020921  -0.013587   0.006545  -0.021390  -0.020891  -0.042904   \n",
      "\n",
      "            000009.SZ  000010.SZ  000011.SZ  000012.SZ  ...  688708.SH  \\\n",
      "date                                                    ...              \n",
      "2002-01-04        NaN        NaN        NaN        NaN  ...        NaN   \n",
      "2002-01-07  -0.020839   0.015152  -0.015525  -0.023666  ...        NaN   \n",
      "2002-01-08  -0.004216  -0.002985  -0.049697  -0.012998  ...        NaN   \n",
      "2002-01-09  -0.017138   0.006986  -0.049755  -0.026338  ...        NaN   \n",
      "2002-01-10   0.006565   0.007929   0.006712   0.005410  ...        NaN   \n",
      "...               ...        ...        ...        ...  ...        ...   \n",
      "2024-12-25  -0.015789  -0.035336  -0.001121  -0.009259  ...        NaN   \n",
      "2024-12-26  -0.007487   0.014652   0.001122  -0.003738  ...        NaN   \n",
      "2024-12-27   0.015086   0.010830   0.012332   0.007505  ...        NaN   \n",
      "2024-12-30  -0.003185  -0.010714  -0.015504  -0.003724  ...        NaN   \n",
      "2024-12-31  -0.025559   0.014440  -0.016873  -0.013084  ...        NaN   \n",
      "\n",
      "            920098.BJ  301617.SZ  688605.SH  920106.BJ  301585.SZ  301598.SZ  \\\n",
      "date                                                                           \n",
      "2002-01-04        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-07        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-08        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-09        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2002-01-10        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "...               ...        ...        ...        ...        ...        ...   \n",
      "2024-12-25        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-26        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-27        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-30        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "2024-12-31        NaN        NaN        NaN        NaN        NaN        NaN   \n",
      "\n",
      "            603194.SH  920082.BJ  001391.SZ  \n",
      "date                                         \n",
      "2002-01-04        NaN        NaN        NaN  \n",
      "2002-01-07        NaN        NaN        NaN  \n",
      "2002-01-08        NaN        NaN        NaN  \n",
      "2002-01-09        NaN        NaN        NaN  \n",
      "2002-01-10        NaN        NaN        NaN  \n",
      "...               ...        ...        ...  \n",
      "2024-12-25        NaN        NaN        NaN  \n",
      "2024-12-26        NaN        NaN        NaN  \n",
      "2024-12-27        NaN        NaN        NaN  \n",
      "2024-12-30        NaN        NaN        NaN  \n",
      "2024-12-31        NaN        NaN        NaN  \n",
      "\n",
      "[5579 rows x 5379 columns]\n"
     ]
    }
   ],
   "source": [
    "close_prices = pd.read_csv(fr'D:\\多任务选股_因子计算\\factor_calculation\\data_processed\\data_close_processed.csv', index_col=0, parse_dates=True)\n",
    "\n",
    "# 计算收益率\n",
    "daily_returns = close_prices.pct_change()\n",
    "\n",
    "\n",
    "print(daily_returns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "近63日收益率以换手率指数衰减加权"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取换手率数据\n",
    "turn_df = pd.read_csv(fr'D:\\多任务选股_因子计算\\factor_calculation\\data_processed\\data_turn_processed.csv', index_col=0, parse_dates=True)\n",
    "\n",
    "\n",
    "# 初始化一个空的 DataFrame 用于存储结果\n",
    "weighted_returns = pd.DataFrame(index=daily_returns.index, columns=daily_returns.columns)\n",
    "\n",
    "# 遍历每只股票\n",
    "for stock in daily_returns.columns:\n",
    "    # 进行 63 日滚动计算\n",
    "    weighted_returns[stock] = daily_returns[stock].rolling(window=63).apply(\n",
    "        lambda x: calculate_weighted_return(x, turn_df[stock].loc[x.index])\n",
    "    )\n",
    "    print(f\"Processed {stock} successfully!\")   \n",
    "# 打印结果\n",
    "print(weighted_returns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading raw data...\n",
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 配置参数\n",
    "DATA_LIST=['turn','close']\n",
    "# 加载数据\n",
    "print(\"Loading raw data...\")\n",
    "long_df = load_raw_data(DATA_LIST)\n",
    "'''\n",
    "start_date = '2002-01-01'\n",
    "end_date = '2002-07-31'\n",
    "long_df = load_raw_data1(DATA_LIST, start_date, end_date)\n",
    "'''\n",
    "close_df = long_df['close']\n",
    "turn_df = long_df['turn']\n",
    "\n",
    "# 计算因子\n",
    "calculator = exp_wgt_return_3m(close_df, turn_df)\n",
    "\n",
    "# 计算所有股票的因子值\n",
    "factor_df = calculator.calculate_63day_return_weighted_factor_all_stocks()\n",
    "\n",
    "# 预处理\n",
    "factor_df = winsorize(factor_df)\n",
    "factor_df = standardlize(factor_df)\n",
    "\n",
    "# 保存结果\n",
    "factor_df.to_pickle('exp_wgt_return_3m.pkl')\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "21 日收益率标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading raw data...\n",
      "Calculating std factor...\n",
      "Calculating all std factors...\n",
      "All std factors calculated successfully!\n"
     ]
    }
   ],
   "source": [
    "# 配置参数\n",
    "DATA_LIST=['close','turn']\n",
    "# 加载数据\n",
    "print(\"Loading raw data...\")\n",
    "#start_date = '2024-01-01'\n",
    "#end_date = '2024-12-31'\n",
    "#long_df = load_raw_data1(DATA_LIST, start_date, end_date)\n",
    "long_df = load_raw_data(DATA_LIST)\n",
    "\n",
    "close_df = long_df['close']\n",
    "turn_df = long_df['turn']\n",
    "# 计算因子\n",
    "calculator = std(close_df, turn_df)\n",
    "print(\"Calculating std factor...\")\n",
    "\n",
    "\n",
    "# 计算所有股票的近 21 日收益率标准差\n",
    "std_df = calculator.calculate_21day_return_std_all_stocks()\n",
    "print(\"Calculating all std factors...\")\n",
    "\n",
    "std_df = winsorize(volume_std)\n",
    "std_df = standardlize(volume_std)\n",
    "# 保存因子值 DataFrame 到 pkl 文件\n",
    "std_df.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\std_1m.pkl')\n",
    "print(\"All std factors calculated successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_LIST=['volume']\n",
    "# 加载数据\n",
    "print(\"Loading raw data...\")\n",
    "volume_df = load_raw_data(DATA_LIST)\n",
    "volume_std = volume_df.rolling(window=21).std()\n",
    "volume_std = winsorize(volume_std)\n",
    "volume_std = standardlize(volume_std)\n",
    "volume_std.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\vstd_1m .pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "换手率近21日均值 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading raw data...\n",
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "DATA_LIST=['turn']\n",
    "# 加载数据\n",
    "print(\"Loading raw data...\")\n",
    "turn_df = load_raw_data(DATA_LIST)\n",
    "# 计算每只股票换手率的近 21 日均值\n",
    "turn_21d_mean = turn_df['turn'].rolling(window=21).mean()\n",
    "# 预处理\n",
    "turn_21d_mean = winsorize(turn_21d_mean)\n",
    "turn_21d_mean = standardlize(turn_21d_mean)\n",
    "turn_21d_mean.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\turn_1m.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "换手率近21日标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算每只股票换手率的近 21 日标准差\n",
    "turn_21d_std = turn_df['turn'].rolling(window=21).std()\n",
    "# 预处理\n",
    "turn_21d_std = winsorize(turn_21d_std)\n",
    "turn_21d_std = standardlize(turn_21d_std)\n",
    "turn_21d_std.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\std_turn_1m.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "换手率近21日均值/近21日标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "mean_std=turn_21d_mean/turn_21d_std\n",
    "mean_std = winsorize(mean_std)\n",
    "mean_std = standardlize(mean_std)\n",
    "mean_std.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\bias_turn_1m .pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "收益率近10日标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "returns_10d_std = daily_returns.rolling(window=10).std()\n",
    "# 预处理\n",
    "returns_10d_std = winsorize(returns_10d_std)\n",
    "returns_10d_std = standardlize(returns_10d_std)\n",
    "\n",
    "# 保存到 pkl 文件\n",
    "returns_10d_std.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\std_ret_10d.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "成交量近10日标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "volume_10d_std = volume_df.rolling(window=10).std()\n",
    "# 预处理\n",
    "volume_10d_std = winsorize(volume_10d_std)\n",
    "volume_10d_std = standardlize(volume_10d_std)\n",
    "\n",
    "# 保存到 pkl 文件\n",
    "volume_10d_std.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\std_vol_10d.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "换手率近10日标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算每只股票换手率的近 10 日均值\n",
    "turn_10d_std = turn_df['turn'].rolling(window=10).std()\n",
    "# 预处理\n",
    "turn_10d_std = winsorize(turn_10d_std)\n",
    "turn_10d_std = standardlize(turn_10d_std)\n",
    "turn_10d_std.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\std_turn_10d.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "收益率和收盘价近10日相关系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    daily_returns[col].rolling(window=10).corr(close_prices[col])\n",
    "    for col in close_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = close_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_ret_close.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "收益率和开盘价近10日相关系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "open_prices = pd.read_csv(fr'D:\\多任务选股_因子计算\\factor_calculation\\data_processed\\data_open_processed.csv', index_col=0, parse_dates=True)\n",
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    daily_returns[col].rolling(window=10).corr(open_prices[col])\n",
    "    for col in open_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = open_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_ret_open.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "收益率和最高价近10日相关系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "high_prices = pd.read_csv(fr'D:\\多任务选股_因子计算\\factor_calculation\\data_processed\\data_high_processed.csv', index_col=0, parse_dates=True)\n",
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    daily_returns[col].rolling(window=10).corr(high_prices[col])\n",
    "    for col in high_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = high_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_ret_high.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "收益率和最低价近10日相关系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "low_prices = pd.read_csv(fr'D:\\多任务选股_因子计算\\factor_calculation\\data_processed\\data_low_processed.csv', index_col=0, parse_dates=True)\n",
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    daily_returns[col].rolling(window=10).corr(low_prices[col])\n",
    "    for col in low_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = low_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_ret_low.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "vwap_prices = pd.read_csv(fr'D:\\多任务选股_因子计算\\factor_calculation\\data_processed\\data_vwap_processed.csv', index_col=0, parse_dates=True)\n",
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    daily_returns[col].rolling(window=10).corr(vwap_prices[col])\n",
    "    for col in vwap_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = vwap_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_ret_vwap.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    daily_returns[col].rolling(window=10).corr(volume_df[col])\n",
    "    for col in volume_df.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = volume_df.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_ret_vol.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "turn=turn_df['turn']\n",
    "correlation = pd.concat([\n",
    "    daily_returns[col].rolling(window=10).corr(turn[col])\n",
    "    for col in turn.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = turn.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_ret_turn.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    volume_df[col].rolling(window=10).corr(close_prices[col])\n",
    "    for col in close_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = close_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_vol_close.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    volume_df[col].rolling(window=10).corr(open_prices[col])\n",
    "    for col in open_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = open_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_vol_open.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    volume_df[col].rolling(window=10).corr(high_prices[col])\n",
    "    for col in high_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = high_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_vol_high.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    volume_df[col].rolling(window=10).corr(low_prices[col])\n",
    "    for col in low_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = low_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_vol_low.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 计算滚动相关系数\n",
    "correlation = pd.concat([\n",
    "    volume_df[col].rolling(window=10).corr(vwap_prices[col])\n",
    "    for col in vwap_prices.columns\n",
    "], axis=1)\n",
    "\n",
    "# 调整结果的列名\n",
    "correlation.columns = vwap_prices.columns\n",
    "\n",
    "# 预处理\n",
    "correlation = winsorize(correlation)\n",
    "correlation = standardlize(correlation)\n",
    "# 保存到 pkl 文件\n",
    "correlation.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\corr_vol_vwap.pkl')\n",
    "\n",
    "print(\"All factors processed successfully!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "日内技术"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "low_high=low_prices / high_prices\n",
    "# 预处理\n",
    "low_high = winsorize(low_high)\n",
    "low_high = standardlize(low_high)\n",
    "low_high.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\low2high.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "vwap_close=vwap_prices / close_prices\n",
    "# 预处理\n",
    "vwap_close = winsorize(vwap_close)\n",
    "vwap_close = standardlize(vwap_close)\n",
    "vwap_close.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\vwap2close.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "kmid=(close_prices-open_prices)/open_prices\n",
    "# 预处理\n",
    "kmid = winsorize(kmid)\n",
    "kmid = standardlize(kmid)\n",
    "kmid.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\kmid.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "klen=(high_prices-low_prices)/low_prices\n",
    "# 预处理\n",
    "klen = winsorize(klen)\n",
    "klen = standardlize(klen)\n",
    "klen.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\klen.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "kmid2=(close_prices-open_prices)/(high_prices-low_prices)\n",
    "# 预处理\n",
    "kmid2 = winsorize(kmid2)\n",
    "kmid2 = standardlize(kmid2)\n",
    "kmid2.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\kmid2.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 获取 open_prices 和 close_prices最大值\n",
    "max_open_close = open_prices.where(open_prices >= close_prices, close_prices)\n",
    "\n",
    "# 计算 kup 值\n",
    "kup = (high_prices - max_open_close) / open_prices\n",
    "\n",
    "# 预处理\n",
    "kup = winsorize(kup)\n",
    "kup = standardlize(kup)\n",
    "kup.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\kup.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "kup2=(high_prices-max_open_close)/(high_prices-low_prices)\n",
    "# 预处理\n",
    "kup2 = winsorize(kup2)\n",
    "kup2 = standardlize(kup2)\n",
    "kup2.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\kup2.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "# 获取 open_prices 和 close_prices最小值\n",
    "min_open_close = open_prices.where(open_prices <= close_prices, close_prices)\n",
    "klow=(min_open_close-low_prices)/open_prices\n",
    "# 预处理\n",
    "klow = winsorize(klow)\n",
    "klow = standardlize(klow)\n",
    "klow.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\klow.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "klow2=(min_open_close-low_prices)/(high_prices-low_prices)\n",
    "# 预处理\n",
    "klow2 = winsorize(klow2)\n",
    "klow2 = standardlize(klow2)\n",
    "klow2.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\klow2.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "ksft=(2*close_prices-high_prices-low_prices)/open_prices\n",
    "# 预处理\n",
    "ksft = winsorize(ksft)\n",
    "ksft = standardlize(ksft)\n",
    "ksft.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\ksft.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All factors processed successfully!\n"
     ]
    }
   ],
   "source": [
    "ksft2=(2*close_prices-high_prices-low_prices)/(high_prices-low_prices) \n",
    "# 预处理\n",
    "ksft2 = winsorize(ksft2)\n",
    "ksft2 = standardlize(ksft2)\n",
    "ksft2.to_pickle(fr'D:\\多任务选股_因子计算\\factor_calculation\\factor\\ksft2.pkl')\n",
    "print(\"All factors processed successfully!\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
